//
//  CXOriginalView.swift
//  Project03-Weibo-swift
//
//  Created by w on 16/1/22.
//  Copyright © 2016年 w. All rights reserved.
//

import UIKit
import SnapKit
import YYText

let CXComponentsInMargin = 8
let CXHeadImageSize = 35

class CXOriginalView: UIView {
    
    var bottomCons:Constraint?
    
    var statusViewModel: CXStatusViewModel? {
        didSet {
            //TODO: 设置各部分内容
            headView.sd_setImageWithURL(NSURL(string: (statusViewModel?.status?.user?.profile_image_url)!), placeholderImage: UIImage(named: "avatar_default")) //下载图片的过程放在这里并不好，但是框架支持的是UIImageView。。。
            nameView.text = statusViewModel?.status?.user?.screen_name
            mbView.image = statusViewModel?.mbImage
            verifiedView.image = statusViewModel?.verifiedImage
//            contentView.text = statusViewModel?.status?.text
            
            if let attrStr = statusViewModel?.originAttrString {
                contentView.attributedText = attrStr
                
                //由于YYText无法根据自身内容调节布局尺寸 因此需要额外借助YYText内容进行设置
                let maxSize = CGSize(width: SCREENW - 2 * CGFloat(CXComponentsInMargin), height: CGFloat(MAXFLOAT))
                let layout = YYTextLayout(containerSize: maxSize, text: attrStr)
                contentView.snp_updateConstraints(closure: { (make) -> Void in
                    make.height.equalTo(layout.textBoundingSize.height)
                })
            }
            
            sourceView.text = statusViewModel?.sourceText
            timeView.text = statusViewModel?.createText
            
            bottomCons?.uninstall()
            if let pic_urls = statusViewModel?.status?.pic_urls where pic_urls.count > 0 {
                //处理底部约束
                photoView.hidden = false
                photoView.pic_urls = pic_urls
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomCons = make.bottom.equalTo(photoView).offset(CXComponentsInMargin).constraint
                })
            } else {
                photoView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomCons = make.bottom.equalTo(contentView).offset(CXComponentsInMargin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //懒加载各控件
    private lazy var headView: UIImageView = UIImageView(image: UIImage(named: "avatar_default"))
    private lazy var nameView: UILabel = {
        let nl = UILabel(textColor: UIColor.darkGrayColor(), font: 14)
        nl.text = "占位"
        return nl
    }()
    //放图片占位
    private lazy var mbView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    /// 创建时间 label
    private lazy var timeView: UILabel = {
        let label = UILabel(textColor: UIColor.orangeColor(), font: 12)
        label.text = "刚刚"
        return label
    }()
    /// 来源
    private lazy var sourceView: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), font: 12)
        label.text = "来自 新浪微博"  //放文字占位
        return label
    }()
    
    /// 认证图标
    private lazy var verifiedView: UIImageView = {
        let image = UIImageView(image: UIImage(named: "avatar_vip"))
        return image
    }()
    
    /// 内容的label
    private lazy var contentView: YYLabel = {
        let label = YYLabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(15.0)
        // 换行
        label.numberOfLines = 0
        return label
    }()
    
    /// 懒加载图片显示view
    private lazy var photoView: CXStatusPhotoView = {
        let photoV = CXStatusPhotoView()   //可以这么去用，只要内部实现了那个必须实现的构造函数
        photoV.backgroundColor = self.backgroundColor
        return photoV
    }()

}

extension CXOriginalView {
    
    private func setupUI() {
        //TODO: 将各部分添加到视图 设置他们的约束
        backgroundColor = UIColor.whiteColor()
        
        addSubview(headView)
        addSubview(nameView)
        addSubview(mbView)
        addSubview(timeView)
        addSubview(sourceView)
        addSubview(verifiedView)
        addSubview(contentView)
        addSubview(photoView)
        
        contentView.highlightTapAction = { (containerView, text, range, rect) -> () in
            // 取到富文本里面对应位置的属性
            let result = text.attribute(YYTextHighlightAttributeName, atIndex: range.location, effectiveRange: nil) as! YYTextHighlight
            print(result.userInfo["content"])
        }

        
        //设置约束
        headView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(CXComponentsInMargin)
            make.top.equalTo(CXComponentsInMargin)
            make.width.height.equalTo(CXHeadImageSize)
        }
        nameView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headView.snp_trailing).offset(CXComponentsInMargin)
            make.top.equalTo(headView)
        }
        mbView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameView)
            make.leading.equalTo(nameView.snp_trailing).offset(CXComponentsInMargin)
        }
        timeView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameView)
            make.bottom.equalTo(headView)
        }
        
        sourceView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeView.snp_trailing).offset(CXComponentsInMargin)
            make.centerY.equalTo(timeView)
        }
        
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headView.snp_trailing).offset(-3)
            make.centerY.equalTo(headView.snp_bottom).offset(-3)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headView)
            make.top.equalTo(headView.snp_bottom).offset(CXComponentsInMargin)
            make.trailing.equalTo(self).offset(-CXComponentsInMargin)
        }
        photoView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.top.equalTo(contentView.snp_bottom).offset(CXComponentsInMargin)
        }
        //最后设置originalView自身的底部约束
        self.snp_makeConstraints { (make) -> Void in
            bottomCons = make.bottom.equalTo(photoView).offset(CXComponentsInMargin).constraint
        }
    }
}
